<?php


namespace app\api\controller\wanlshop;


use app\common\controller\Api;

class Idcard  extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /**
     * 获取身份证列表
    */
    public function getidcard(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $list = model('app\api\model\wanlshop\Idcard')
            ->where('user_id', $this->auth->id)
            ->field('id,user_id,shop_id,name,number,image_front,image_back')
            ->order('updatetime desc')
            ->paginate();
        $this->success('返回成功', $list);
    }


    /**
     * 修改/新增身份证
     *
     * @ApiSummary  (WanlShop 地址接口修改/新增地址)
     * @ApiMethod   (POST)
     *
     * @param string $user_id 用户ID
     */
    public function idcard()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $request = $this->request->post();

            $address = new \app\api\model\wanlshop\Idcard();
            $data = $request['data'];
            $data['user_id'] = $this->auth->id;
            // 操作
            switch ($request['type']) {
                case "edit":
                    $address->allowField(true)->save($data,['id' => $data['id']]);
                    $this->success('ok');
                    break;
                case "add":
                    $address->data($data);
                    $address->save();
                    $this->success('地址回调',$address);
                    break;
            }
        } else {
            $this->error(__('Illegal request'));
        }
    }

    /**
     * 删除身份证
     */
    public function delidcard()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $id = $this->request->post('id');
            if (!$id) {
                $this->error(__('Invalid parameters'));
            }
            if(model('app\api\model\wanlshop\Idcard')->where(['id'=>$id,'user_id'=>$this->auth->id])->delete()){
                $this->success(__('Delete the success',[]));
            }else{
                $this->error(__('Delete failed'));
            }
        } else {
            $this->error(__('Illegal request'));
        }
    }
}